package org.ing.sisval.da;

import java.sql.Connection;
import java.util.ArrayList;
import org.ing.sisval.be.BEValcomplementario;
import org.rx.cr.dao.DAOAbstract;

public class DAValcomplementario extends DAOAbstract<BEValcomplementario> {

    public DAValcomplementario(Connection conn) {
        setConnection(conn);
    }

    @Override
    public int registrar(BEValcomplementario be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_ins_valcomplementario");
            setParameterInt(be.getValuacioncomplementarioid());
            setParameterInt(be.getComplementarioid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BEValcomplementario be) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_upd_valcomplementario");
            setParameterInt(be.getValcomplementarioid());
            setParameterInt(be.getValuacioncomplementarioid());
            setParameterInt(be.getComplementarioid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    public int eliminarRegistro(int valcomplementarioid) throws Exception {
        try {
            setStoreProcedure("SISVAL.sp_del_valcomplementario");
            setParameterInt(valcomplementarioid);
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BEValcomplementario> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_valcomplementario");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BEValcomplementario be = new BEValcomplementario();
                be.setValcomplementarioid(getDataInt(1));
                be.setValuacioncomplementarioid(getDataInt(2));
                be.setComplementarioid(getDataInt(3));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    public ArrayList<BEValcomplementario> listar(int id) throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_valcomplementario");
            setParameterInt(id);
            commitc();
            while (existResult()) {
                BEValcomplementario be = new BEValcomplementario();
                be.setValcomplementarioid(getDataInt(1));
                be.setValuacioncomplementarioid(getDataInt(2));
                be.setComplementarioid(getDataInt(3));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BEValcomplementario buscar(int i) throws Exception {
        try {
            BEValcomplementario be = null;
            setStoreProcedure("SISVAL.sp_sel_valcomplementario_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BEValcomplementario();
                be.setValcomplementarioid(getDataInt(1));
                be.setValuacioncomplementarioid(getDataInt(2));
                be.setComplementarioid(getDataInt(3));

            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BEValcomplementario> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("SISVAL.sp_sel_valcomplementario");
            setParameterString("");
            commitc();
            while (existResult()) {
                BEValcomplementario be = new BEValcomplementario();
                be.setValcomplementarioid(getDataInt(1));
                be.setValuacioncomplementarioid(getDataInt(2));
                be.setComplementarioid(getDataInt(3));
                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public int eliminarRegistro(BEValcomplementario tipo) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public ArrayList<BEValcomplementario> listarAsignacion(int complementarioId) {
        try {
            clear();
            setStoreProcedure("sp_sel_asignacion_complementario");
            setParameterInt(complementarioId);
            commitc();
            while (existResult()) {
                BEValcomplementario be = new BEValcomplementario();
                be.setValcomplementarioid(getDataInt(1));
                be.setValuacioncomplementarioid(getDataInt(2));
                be.setComplementarioid(getDataInt(3));

                add(be);

            }
            return getLista();

        } catch (Exception e) {
            try {
                rollback(e);
            } catch (Exception ex) {
            }
            return null;
        }
    }

    public int eliminarAsignacion(int complementarioId) throws Exception {
        try {
            setStoreProcedure("sp_del_valcomplementario");
            setParameterInt(complementarioId);
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

}
